package com.fadadianqi.logistics.sell.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.logistics.sell.dao.InOutDAO;
import com.fadadianqi.logistics.sell.model.InOut;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.Pagination.Order;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InOutDAOImpl extends GenericDAOSuport<String, InOut> implements InOutDAO {

    @Override
    protected Class<?> getObjectClass() {
        return InOut.class;
    }

    @Override
    protected String getTableName() {
        return "in_out";
    }

    @Override
    public int countAll() {
        return super.countAll(null);
    }

    @Override
    public double totalMoney(int year) {
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumMoney", year);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<InOut> findBetween(Date date1, Date date2, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        if (pagination != null) {
            map.put(START_ROW_NUM, pagination.getStartRows());
            map.put(MAX_ROW_NUM, pagination.getMaxResults());
            map.put(END_ROW_NUM, pagination.getStartRows() + pagination.getMaxResults());
            List<Order> orderList = pagination.getOrderList();
            if (ValidatorHelper.isNotEmpty(orderList)) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < orderList.size(); i++) {
                    Order order = orderList.get(i);
                    sb.append(order.getName());
                    if (order.getAscending()) {
                        sb.append(" ASC");
                    } else {
                        sb.append(" DESC");
                    }
                    if (i != (orderList.size() - 1)) {
                        sb.append(",");
                    }
                }
                map.put(ORDER_STR, sb.toString());
            }
        }
        return (List<InOut>) getSqlMapClientTemplate().queryForList(getNamespace() + ".findBetween", map);
    }

    @Override
    public Integer countBetween(Date date1, Date date2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        Integer i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countBetween", map);
        return i;
    }

    @Override
    public Double sumMoneyBetween(Date date1, Date date2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumMoneyBetween", map);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public double inMoney(int year) {
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".inMoney", year);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

    @Override
    public double outMoney(int year) {
        Object d = getSqlMapClientTemplate().queryForObject(getNamespace() + ".outMoney", year);
        if (d != null) {
            return (Double) d;
        } else {
            return 0D;
        }
    }

}
